package com.james.happyfarm.servlets;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.james.happyfarm.JDO.Message;
import com.james.happyfarm.JDO.PMF;

public class CleanupMessage extends HttpServlet {

	private static final long serialVersionUID = 6964576015034025880L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Message.class, "date < :yesterday");
		Date yesterday = new Date(Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")).getTimeInMillis() - 2*24*3600*1000);
		Long l = query.deletePersistentAll((Object)yesterday);
		resp.getWriter().write(l.toString());
	}

}
